<?php
namespace Home\Model;
use Common\Model\BaseModel;

class ContractModel extends BaseModel {

    public $tableName = 'contract';

    protected $_validate = array(
        array('contract_id','require','合同编号不能为空!'),
        array('contract_id','','合同编号已经存在！',0,'unique',1),
    );

    /**
     * @param $post
     * @return array
     * 更新或修改合同信息
     */
    public function saveContract($data){

        $ret = array('status'=>true,'msg'=>'');
        $post = $this->returnData($data);

        if(!$this->create($post)){

            $ret['status'] = false;
            $ret['msg'] = $this->getError();

        }else{

            $aore = $this->where("contract_id='".$post['contract_id']."' and del=1")->select();
            $res = !empty($aore) ? $this->save($post) : $this->add($post);

            if(false !== $res){
                $ret['msg'] = '操作成功';
            }else{
                $ret['status'] = false;
                $ret['msg'] = $this->getDbError();
            }
        }

         return $ret;
    }

    public function returnData($post){

        $return = array();

        $return['contract_id']          = !empty($post['contract_id']) ? addslashes($post['contract_id']) : null;
        $return['company_name']         = !empty($post['company_name']) ? addslashes($post['company_name']) : '';
        $return['amount']               = !empty($post['amount']) ? doubleval($post['amount']) : '';
        $return['make_time']            = !empty($post['make_time']) ? strtotime($post['make_time']) : time();
        $return['end_time']             = !empty($post['end_time']) ? strtotime($post['end_time']) : time();
        $return['content']              = !empty($post['content']) ? addslashes($post['content']) : '';
        $return['extern']               = !empty($post['extern']) ? intval($post['extern']) : 1;
        $return['reg_time']             = !empty($post['reg_time']) ? strtotime($post['reg_time']) : time();
        $return['del']                  = 1;

        return $return;
    }
    public function getContractList(){

        $list = $this->field('contract_id,company_name')->where('del=1')->select();
        
        return !empty($list) ? $list : array();
    }

}